<?php
namespace Tests;

use Laravel\Lumen\Testing\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    public function createApplication()
    {
        return include __DIR__.'/../bootstrap/app.php';
    }

    /**
     * 打印请求URL和返回结果
     */
    public function printRequest()
    {
        echo $this->response->getContent() . "\n\n";
    }

    /**
     * 打印请求URL
     *
     * @param  string $method
     * @param  string $uri
     * @param  array  $data
     * @param  array  $headers
     * @return $this
     */
    public function json($method, $uri, array $data = [], array $headers = [])
    {
        //  echo sprintf("\n[%s] %s -- %s\n", $method, $uri, json_encode($data));
        parent::json($method, $uri, $data, $headers);
        return $this;
    }

    public function checkStatusAndStructure($dataStructure = null)
    {
        // $this->printRequest();
        $this->seeStatusCode(200);
        $structure = [
            'code',
            'info',
            'data',
        ];
        if ($dataStructure) {
            $structure['data'] = $dataStructure;
        }
        $this->seeJsonStructure($structure);
        $this->assertEquals(200, $this->response->getOriginalContent()['code']);
    }
    /**
     * signature
     *
     * @param mixed $token
     * @param mixed $timestamp
     * @param mixed $nonce
     *
     * @access public
     *
     * @return mixed
     */
    public function signature($token, $timestamp, $nonce)
    {
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        return sha1(implode('', $tmpArr));
    }
    /**
     * genAuth
     *
     * @access public
     *
     * @return mixed
     */
    public function genAuth()
    {
        $timestamp = time();
        $nonce     = $this->genRandStr(8);
        $token     = '0123456789abcdef';
        $signature = $this->signature($token, $timestamp, $nonce);

        return ['app_id'=>'112231', 'nonce'=> $nonce, 'signature'=>$signature, 'timestamp'=>$timestamp];
    }
    /**
     * genRandStr
     *
     * @param mixed $length
     *
     * @access public
     *
     * @return mixed
     */
    public function genRandStr($length)
    {
        $str = null;
        $strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;

        for ($i=0; $i < $length; $i++) {
            $str.= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }
    /**
     * call
     *
     * @param mixed $method
     * @param mixed $uri
     * @param mixed $parameters
     * @param mixed $cookies
     * @param mixed $files
     * @param mixed $server
     * @param mixed $content
     *
     * @access public
     *
     * @return mixed
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
         $server = array_merge($server, $this->transformHeadersToServerVars($this->genAuth()));
         return parent :: call($method, $uri, $parameters, $cookies, $files, $server, $content);
    }
}
